﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Media;
using DeepEarth.Map.Core;
using DeepEarth.Map.Core.Tiling; 

#if !WINDOWS_PHONE
using System.ComponentModel.Composition;

#endif

namespace DeepEarth.Map.TileSources
{
#if !WINDOWS_PHONE
    [Export(typeof(BaseTileSource))] 
#endif
    public sealed class OSMTileSource: BaseTileSource
    {
        private const int DefaultMaxZoomLevel = 21;
        private const int DefaultTileSize = 256;
        private const string TilePath = @"http://{0}.tile.openstreetmap.org/{1}/{2}/{3}.png";
        static int cycle = 0;
        static char[] request = { 'a', 'b', 'c' };

        public OSMTileSource()
            : base(OSMTileSourceFactory.StreetMap, (int)Math.Pow(2, DefaultMaxZoomLevel) * DefaultTileSize,
                   (int)Math.Pow(2, DefaultMaxZoomLevel) * DefaultTileSize,
                   DefaultTileSize,
                   DefaultTileSize,
                    0)
        {
        }

        internal OSMTileSource(MapMode mode)
            : base(mode, (int)Math.Pow(2, DefaultMaxZoomLevel) * DefaultTileSize,
                (int)Math.Pow(2, DefaultMaxZoomLevel) * DefaultTileSize,
                DefaultTileSize,
                DefaultTileSize,
                0)
        {
        }

        protected override string GetTileUri(int zoom, int tilePositionX, int tilePositionY)
        {
            return string.Format(TilePath, request[(cycle++) % 3], zoom, tilePositionX, tilePositionY);
        }
	}
}